package com.xmy.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.erp.entity.*;
import com.xmy.erp.service.IGradeService;
import com.xmy.erp.service.IQuarterService;
import com.xmy.erp.service.IStudentAccountService;
import com.xmy.erp.service.IStudentGradeService;
import com.xmy.study.respons.ResStudentClassType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2023-09-09
 */
@Controller
@RequestMapping("/api/erp/studentAccount")
public class StudentAccountController {
    @Autowired
    IStudentAccountService iStudentAccountService;
    @Autowired
    IQuarterService iQuarterService;
    @Autowired
    IStudentGradeService iStudentGradeService;

    @Autowired
    IGradeService iGradeService;

    @GetMapping("/accountIsAt")
    public Long accountIsAt(@RequestParam("account") String account){
        QueryWrapper<StudentAccount> studentAccountQueryWrapper = new QueryWrapper<>();
        studentAccountQueryWrapper.eq("account",account);
        studentAccountQueryWrapper.last("limit 1");
        Long count = iStudentAccountService.count(studentAccountQueryWrapper);
        return count;
    }

    @GetMapping("/login")
    public StudentAccount login(@RequestParam("account") String account,@RequestParam("pwd") String pwd){
        QueryWrapper<StudentAccount> studentAccountQueryWrapper = new QueryWrapper<>();
        studentAccountQueryWrapper.eq("account",account);
        studentAccountQueryWrapper.eq("pwd",pwd);
        studentAccountQueryWrapper.last("limit 1");
        StudentAccount studentAccount = iStudentAccountService.getOne(studentAccountQueryWrapper);
        return studentAccount;
    }

    @GetMapping("/getClassType")
    public List<ResStudentClassType> getStudentClassType(@RequestParam("studentId") String studentId){
        StudentGrade studentGrade = new StudentGrade();
        Quarter quarter = iQuarterService.getNowQuarter();
        Long[] dataArr = new Long[]{1L,2L,3L};
        List<ResStudentClassType> studentClassTypeList = new ArrayList<>();
        for (Long num:dataArr){
            QueryWrapper<StudentGrade> studentGradeQueryWrapper = new QueryWrapper<>();
            studentGradeQueryWrapper.eq("student_id",studentId);
            studentGradeQueryWrapper.eq("year_part",quarter.getYearPart());
            studentGradeQueryWrapper.eq("quarter_num",quarter.getNum());
            studentGradeQueryWrapper.eq("status",1);
            studentGradeQueryWrapper.eq("reading_status",1);
            studentGradeQueryWrapper.eq("subjects_id",num);
            studentGradeQueryWrapper.orderByDesc("id");
            studentGradeQueryWrapper.last("limit 1");
            StudentGrade studentGradeInfo = iStudentGradeService.getOne(studentGradeQueryWrapper);
            if (studentGradeInfo!=null){
                Grade grade = iGradeService.getById(studentGradeInfo.getGradeId());
                if (grade!=null){
                    ResStudentClassType studentClassType = new ResStudentClassType();
                    studentClassType.setSchoolId(studentGrade.getSchoolId());
                    studentClassType.setYearPart(quarter.getYearPart());
                    studentClassType.setQuarterNum(quarter.getNum());
                    studentClassType.setSubjectsId(num);
                    studentClassType.setStudentId(studentGradeInfo.getStudentId());
                    studentClassType.setClassTypeId(grade.getCourseTypeId());
                    studentClassType.setGradeId(studentGrade.getGradeId());
                    studentClassType.setYearClassId(grade.getYearClassId());
                    studentClassTypeList.add(studentClassType);
                }
            }
        }
        return studentClassTypeList;
    }
}
